﻿/// <reference path="../jquery-3.4.1.min.js" />
/// <reference path="../common.js" />
/// <reference path="../ArrayHelper.js" />

var uploader = null;

$(function () {
    BindEvents();
    DataInit();
});

function BindEvents() {

    //换班
    $('#btnChangeSchedule,#btnChangeScheduleBottom').click(function () {
        if ($('#dataTable tbody .btn.selected').length == 2) {
            var dataArr = [];
            $('#dataTable tbody .btn.selected').each(function () {
                var _that = $(this);

                var workItem = _that.parent();
                var SwitchItem = { WorkDate: workItem.attr('data-date'), UserId: _that.attr('data-uid'), WorkType: workItem.attr('data-type'), Place: workItem.attr('data-place'), UserName:_that.text() };
                dataArr.push(SwitchItem);
            });

            var postData = { 'rtype': 'SwitchPersonSchedule', 'switchObject1': dataArr[0], 'switchObject2': dataArr[1] };
            console.log('换班前台数据：');
            console.log(postData);

            $.rajax('handler/scheduleManagement.ashx', postData, function (retobj) {
                top.sysAlert.success(retobj.msg);
                var tmpPageSize = $('#pager').getPager().PageSize;
                var tmpPageIndex = $('#pager').getPager().PageIndex;
                GetMainList(tmpPageIndex, tmpPageSize);
            });
        }
        else {
            top.sysAlert.Error('换班需要选中2个不同的人');
        }
    });

    //查询
    $('#btnSearch').click(function () {
        RefleshMainList();
    });

    //自动排班事件
    $('#btnAutoSchedule').click(function () {
        var startTime = $.trim($('#divPrevAutoSchedule [data-col="AutoStartTime"]').val());
        var endTime = $.trim($('#divPrevAutoSchedule [data-col="AutoEndTime"]').val());
        if ($.isNullOrUndefinedOrEmpty(startTime)) {
            top.sysAlert.Error('开始时间不能为空');
            return;
        }
        if ($.isNullOrUndefinedOrEmpty(endTime)) {
            top.sysAlert.Error('结束时间不能为空');
            return;
        }

        var postData = $('#divPrevAutoSchedule').getData('AutoScheduleCenterWork');
        $.rajax('handler/scheduleManagement.ashx', postData, function (retobj) {
            top.sysAlert.success(retobj.msg);
            GetMainList(1, $('#pager').getPager().PageSize);
        });
        $('#myModal').modal('hide');
    });

    //网信办自动排班事件
    $('#btnAutoScheduleOffice').click(function () {
        var startTime = $.trim($('#divPrevAutoScheduleOffice [data-col="AutoStartTime"]').val());
        var endTime = $.trim($('#divPrevAutoScheduleOffice [data-col="AutoEndTime"]').val());
        if ($.isNullOrUndefinedOrEmpty(startTime)) {
            top.sysAlert.Error('开始时间不能为空');
            return;
        }
        if ($.isNullOrUndefinedOrEmpty(endTime)) {
            top.sysAlert.Error('结束时间不能为空');
            return;
        }

        var postData = $('#divPrevAutoScheduleOffice').getData('TestOfficeSchedule');
        $.rajax('handler/scheduleManagement.ashx', postData, function (retobj) {
            top.sysAlert.success(retobj.msg);
            GetMainList(1, $('#pager').getPager().PageSize);
        });
        $('#myModalOffice').modal('hide');
    });

    //新增网信办排班
    $('#btnAdd,#btnAddBottom').click(function () {
        sysAlert.ShowIframe('scheduleEdit.aspx?p=2&t=2&m=0', '新增网信办排班', 650, 760);
    });

    //新增中心排班
    $('#btnAddCenterSchedule,#btnAddCenterScheduleBottom').click(function () {
        sysAlert.ShowIframe('scheduleEdit.aspx?p=1&t=1&m=0', '新增中心排班', 650, 760);
    });

    //导出
    $('#btnExportExecute').click(function () {
        var startTime = $.trim($('#divPrevExport [data-col="AutoStartTime"]').val());
        var endTime = $.trim($('#divPrevExport [data-col="AutoEndTime"]').val());
        if ($.isNullOrUndefinedOrEmpty(startTime)) {
            top.sysAlert.Error('开始时间不能为空');
            return;
        }
        if ($.isNullOrUndefinedOrEmpty(endTime)) {
            top.sysAlert.Error('结束时间不能为空');
            return;
        }

        var postData = $('#divPrevExport').getData('ExportSchedule');
        $.rajax('handler/scheduleManagement.ashx', postData, function (retobj) {
            var downUrl = 'handler/FileDownload.ashx?fileName=' + retobj.data;
            window.open(downUrl);
        });
        $('#exportModal').modal('hide');
    });

    $('#btnOfficeScheduleImportBottom').click(function () {
        document.getElementById('btnOfficeScheduleImport').click();
    });

    uploader = initUpload('xls,xlsx', 'btnOfficeScheduleImport', null, function () {
        var attachmentArr = uploader.attachmentList;

        $.rajax('handler/scheduleManagement.ashx', { 'rtype': 'ImportSchedules', 'fileName': attachmentArr[0].fileName }, function (retobj) {
            RefleshMainList();
        });
    });
}

//刷新主列表
function RefleshMainList() {
    var tmpPageSize = $('#pager').getPager().PageSize;
    GetMainList(1, tmpPageSize);
}

function DataInit() {
    var currDate = $('#hidCurrentDate').val();
    $('#SearchStartTime').val(currDate);

    $('[data-col="SearchWorkPlace"]').bindSysCodeDropdownList('2', function () {
        GetMainList(1, 15);
    });
}
//获得页面主列表
function GetMainList(vPageIndex, vPageSize) {

    var postData = $('#searchBlock').getData('GetSchedulePagedList');

    postData.pgIndex = vPageIndex;
    postData.pgSize = vPageSize;

    $.rajax('handler/scheduleManagement.ashx', postData, function (retobj) {
        var dataList = retobj.data.DataList;
        console.log(dataList);
        for (var i = 0; i < dataList.length; i++) {
            var item = dataList[i];

            item.DateCol = GetMainListDateColumn(item);
            item.CenterCol = GetMainListCenterColumn(item);
            if (item.DayOfWeekStr == '星期一' || item.DayOfWeekStr == '星期二' || item.DayOfWeekStr == '星期三' || item.DayOfWeekStr == '星期四' || item.DayOfWeekStr == '星期五') {
                item.OfficeCol = GetMainListOfficeColumn1_5(item);
            }
            else {
                item.OfficeCol = GetMainListOfficeColumn67(item);
            }
        }

        $('#dataTable').bindTable(dataList);
        BindPersonClickEvent();

        $('#pager').setPager(retobj.data.PageInfo, GetMainList);
    });
}

//绑定人员点击事件
function BindPersonClickEvent() {

    function HandleMirror(jqButton, isSelected) {
        $('#dataTable tbody .btn.btn-sm').not(jqButton).each(function () {
            if ($(this).text() == jqButton.text()) {
                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected')
                }

                if (isSelected) {
                    $(this).addClass('selectedMirror');
                }
                else {
                    $(this).removeClass('selectedMirror');
                }
            }
        });
    }

    $('#dataTable tbody .btn.btn-sm').click(function () {
        //处理原来选中的
        //var oldButton = $('#dataTable tbody .btn.btn-sm.selected');
        //if (oldButton.length > 0) {
        //    oldButton.removeClass('selected');
        //    HandleMirror(oldButton, false);
        //}
        
        var theButton = $(this);
        if (theButton.is('.selectedMirror')) {
            theButton.removeClass('selectedMirror');
        }
        //处理按钮本身
        if (theButton.is('.selected')) {
            theButton.removeClass('selected');
        }
        else {
            theButton.addClass('selected');
        }

        HandleMirror(theButton, theButton.is('.selected'));
    });


}

//拼第一列星期几
function GetMainListDateColumn(dataItem) {
    var thtml = '<div class="card">'
	                + '<div class="card-header text-center">'
		                + '<h4>' + dataItem.WorkDateStr + '</h4>'
		                + '<h4>' + dataItem.DayOfWeekStr + '</h4>'
	                + '</div>'
                + '</div>';
    return thtml;
}

//列表每个班的双击事件
function WorkEdit(obj) {
    var vDate = $(obj).attr('data-date');
    var vPlace = $(obj).attr('data-place');
    var vType = $(obj).attr('data-type');

    sysAlert.ShowIframe('scheduleEdit.aspx?m=1&d=' + vDate + '&p=' + vPlace + '&t=' + vType, '编辑排班', 650, 760);
}

//拼中心列的html
function GetMainListCenterColumn(dataItem) {
    var thtml = '<div class="card">'
	            + '<div ondblclick="WorkEdit(this)" data-date="' + dataItem.WorkDateStr + '" data-place="1" data-type="1" class="card-header p-1 bg-white">'
		            + '<span class="btn btn-info">' + dataItem.CenterDaylightWork.WorkTypeMeaning + '</span>';

    if (!$.isNullOrUndefinedOrEmpty(dataItem.CenterDaylightWork.PersonList)) {
        for (var j = 0; j < dataItem.CenterDaylightWork.PersonList.length; j++) {
            var ro = dataItem.CenterDaylightWork.PersonList[j];
            var genderIconPath = ro.Gender == 1 ? '/img/male.png' : '/img/female.png';
            thtml += '<button type="button" data-uid="' + ro.UserId + '" class="btn btn-outline-secondary btn-sm ml-2">' + ro.UserName + '<img class="gender" src="' + genderIconPath + '" /></button>';
        }
    }
    else {
        thtml += '<span class="btn border-white text-info">未排班</span>';
    }

    thtml += '</div>';

    thtml += '<div ondblclick="WorkEdit(this)" data-date="' + dataItem.WorkDateStr + '" data-place="1" data-type="2" class="card-body bg-secondary text-white p-1">'
		    + '<span class="btn btn-light">' + dataItem.CenterNightWork.WorkTypeMeaning + '</span>';

    if (!$.isNullOrUndefinedOrEmpty(dataItem.CenterNightWork.PersonList)) {
        for (var j = 0; j < dataItem.CenterNightWork.PersonList.length; j++) {
            var ro = dataItem.CenterNightWork.PersonList[j];
            var genderIconPath = ro.Gender == 1 ? '/img/male.png' : '/img/female.png';
            thtml += '<button type="button" data-uid="' + ro.UserId + '" class="btn btn-outline-light btn-sm ml-2">' + ro.UserName + '<img class="gender" src="' + genderIconPath + '" /></button>';
        }
    }
    else {
        thtml += '<span class="btn border-secondary text-light">未排班</span>';
    }

    thtml += '</div>'
    thtml += '</div>';

    return thtml;
}

//拼网信办列的周1-5html
function GetMainListOfficeColumn1_5(dataItem) {
    var thtml = '<div class="card">'
	                + '<div class="card-header p-1 bg-light">'
		                + '<span class="btn btn-light text-dark">不需要排班</span>'
	                + '</div>'
	                + '<div ondblclick="WorkEdit(this)" data-date="' + dataItem.WorkDateStr + '" data-place="2" data-type="2"  class="card-body bg-secondary text-white p-1">'
		                + '<span class="btn btn-light">' + dataItem.OfficeNightWork.WorkTypeMeaning + '</span>'

    if (!$.isNullOrUndefinedOrEmpty(dataItem.OfficeNightWork.PersonList)) {
        for (var j = 0; j < dataItem.OfficeNightWork.PersonList.length; j++) {
            var ro = dataItem.OfficeNightWork.PersonList[j];
            var genderIconPath = ro.Gender == 1 ? '/img/male.png' : '/img/female.png';
            thtml += '<button type="button" data-uid="' + ro.UserId + '" class="btn btn-outline-light btn-sm ml-2">' + ro.UserName + '<img class="gender" src="' + genderIconPath + '" /></button>';
        }
    }
    else {
        if (!dataItem.OfficeNightWork.IsNeed) {
            thtml += '<span class="btn border-secondary text-light">未安排中心人员</span>';
        }
        else {
            thtml += '<span class="btn border-secondary text-light">未排班</span>';
        }
    }

    thtml += '</div>'
            + '</div>';

    return thtml;
}

//拼网信办列的周6-7html
function GetMainListOfficeColumn67(dataItem) {
    var thtml = '<div class="card">'
	            + '<div ondblclick="WorkEdit(this)" data-date="' + dataItem.WorkDateStr + '" data-place="2" data-type="1" class="card-header p-1 bg-white">'
		            + '<span class="btn btn-info">' + dataItem.OfficeDaylightWork.WorkTypeMeaning + '</span>';

    if (!$.isNullOrUndefinedOrEmpty(dataItem.OfficeDaylightWork.PersonList)) {
        for (var j = 0; j < dataItem.OfficeDaylightWork.PersonList.length; j++) {
            var ro = dataItem.OfficeDaylightWork.PersonList[j];
            var genderIconPath = ro.Gender == 1 ? '/img/male.png' : '/img/female.png';
            thtml += '<button type="button" data-uid="' + ro.UserId + '" class="btn btn-outline-secondary btn-sm ml-2">' + ro.UserName + '<img class="gender" src="' + genderIconPath + '" /></button>';
        }
    }
    else {
        if (!dataItem.OfficeDaylightWork.IsNeed) {
            thtml += '<span class="btn border-white text-dark">未安排中心人员</span>';
        }
        else {
            thtml += '<span class="btn border-white text-info">未排班</span>';
        }
    }

    thtml += '</div>';

    thtml += '<div ondblclick="WorkEdit(this)" data-date="' + dataItem.WorkDateStr + '" data-place="2" data-type="2" class="card-body bg-secondary text-white p-1">'
		    + '<span class="btn btn-light">' + dataItem.OfficeNightWork.WorkTypeMeaning + '</span>';

    if (!$.isNullOrUndefinedOrEmpty(dataItem.OfficeNightWork.PersonList)) {
        for (var j = 0; j < dataItem.OfficeNightWork.PersonList.length; j++) {
            var ro = dataItem.OfficeNightWork.PersonList[j];
            var genderIconPath = ro.Gender == 1 ? '/img/male.png' : '/img/female.png';
            thtml += '<button type="button" data-uid="' + ro.UserId + '" class="btn btn-outline-light btn-sm ml-2">' + ro.UserName + '<img class="gender" src="' + genderIconPath + '" /></button>';
        }
    }
    else {
        if (!dataItem.OfficeNightWork.IsNeed) {
            thtml += '<span class="btn border-secondary text-light">未安排中心人员</span>';
        }
        else {
            thtml += '<span class="btn border-secondary text-light">未排班</span>';
        }
    }

    thtml += '</div>'
    thtml += '</div>';

    return thtml;
}



